package net.hancl.modules.sys.service.impl;

import net.hancl.common.bean.page.Query;
import net.hancl.common.bean.page.QueryResult;
import net.hancl.modules.sys.dao.DictMapper;
import net.hancl.modules.sys.entity.Dict;
import net.hancl.modules.sys.exception.ObjectExistException;
import net.hancl.modules.sys.service.IDictService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class DictService implements IDictService {

    @Autowired
    private DictMapper dictMapper;

    public QueryResult<Dict> getList(int page, int pagesize, Dict dict) {
        Query query = new Query();
        query.setBean(dict);
        query.setPageIndex(page);
        query.setPageSize(pagesize);

        // 组合分页信息
        QueryResult<Dict> queryResult = new QueryResult<Dict>();
        Long count = dictMapper.getCount(query);
        List<Dict> list = dictMapper.queryPages(query);

        // 总页数 和 取多少条
        queryResult.setCurrentPage(page);
        queryResult.setPages(count, pagesize);
        queryResult.setItems(list);
        return queryResult;
    }

    public Dict get(int id) {
        return dictMapper.selectByPrimaryKey(id);
    }

    public int del(int id) {
        return dictMapper.deleteByPrimaryKey(id);
    }

    public int add(Dict dict) throws Exception {

        if(dict==null||StringUtils.isBlank(dict.getType())||StringUtils.isBlank(dict.getName())||StringUtils.isBlank(dict.getValue())){
            return 0;
        }
        List<Dict> dicts=dictMapper.getByType(dict.getType());
        if(dicts!=null&&dicts.size()>0)
        {
            for(Dict d:dicts){
                if(dict.getName().equals(d.getName())||dict.getValue().equals(d.getValue())){
                    throw new ObjectExistException(dict.getType()+"名称和值已存在！");
                }
            }
        }




        return dictMapper.insertSelective(dict);
    }

    public int update(Dict dict) throws ObjectExistException {

        if(dict==null||StringUtils.isBlank(dict.getType())||StringUtils.isBlank(dict.getName())||StringUtils.isBlank(dict.getValue())){
            return 0;
        }
        List<Dict> dicts=dictMapper.getByType(dict.getType());
        if(dicts!=null&&dicts.size()>0)
        {
            for(Dict d:dicts){
                if(dict.getId()==d.getId()){
                    continue;
                }
                if(dict.getName().equals(d.getName())||dict.getValue().equals(d.getValue())){
                    throw new ObjectExistException(dict.getType()+"名称和值已存在！");
                }
            }
        }

        return dictMapper.updateByPrimaryKeySelective(dict);
    }

    public List<Dict> getByType(String type) {
        return dictMapper.getByType(type);
    }

    public Map<String, String> getByTypeMaps(String type) {
        Map<String, String> mpas=new LinkedHashMap<String, String>();
        List<Dict>  dicts= dictMapper.getByType(type);
        if(dicts!=null&&dicts.size()>0){
            for(Dict dict:dicts){
                mpas.put(dict.getValue(), dict.getName());
            }
        }

        return mpas;
    }
}
